package com.kettle.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableWebMvc
@EnableSwagger2
@ComponentScan(basePackages="com.kettle.controller")
public class SwaggerConfig implements WebMvcConfigurer {
	
    @Value("${swagger.show}")
    private boolean swaggerShow;
	
	@Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
        		.enable(swaggerShow)//是否开启api
        		.apiInfo(this.apiInfo())
        		.select()
        		.apis(RequestHandlerSelectors.basePackage("com.kettle.controller"))
        		.paths(PathSelectors.any())
        		.build();
    }

    private ApiInfo apiInfo() {
    	Contact kcontact = new Contact("king", "#", "#");
        return new ApiInfoBuilder()
        		.title("定时同步数据接口API")
                .description("定时同步数据接口API")
                .termsOfServiceUrl("http://127.0.0.1:8080/ssm_kettle")
                .contact(kcontact)
                .version("1.1.1")
                .build();
    }

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
		registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
		registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
		registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
	}
}
